const app = getApp();
const req = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    page: 1,
    size: 15,
    total: '',
    newList: [], //列表
    statistics: [],
    headerTime: '',
    typeId:0,
    fields: 'day',
    payType: '',
    index: 0,
    showTime: false,
    storeId: '', //门店id
    billId: '',//账单ID
    isPickerShow: false,
    array: [],
    params: {},
    current: '0',
    itemDate: '',
    orderStatus: '',
    
  },


  // ----详情列表------
  transaction(page, size, params) {
    let t = this
    req.wxReq(`/api/statistics/getClerkOrderGroupList/${page}/${size}`, "GET", params).then(res => {
      let {
        list,
        statistics
      } = t.data
      let result = res.data.result
      if (res.data.code == 200) {
        list = result.page.records
        let newList = t.data.newList.concat(list)
        t.setData({
          newList,
          statistics: result.statistics,
          total: result.page.total,
          showModalStatus: false
        })
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 500
        })
      }
    })
  },

  // --点击查询---
  filtrateClik(e) {
    let t = this
    let page = 1
    let {
      size,
      storeId
    } = t.data
   
    let params = e.detail.params
    params.storeId = storeId
    t.setData({
      params,
      newList: []
    })
    t.transaction(page, size, params)

  },

  // --跳转详情页---
  detailsClick(e) {
    let t = this
    let storeUserId = e.currentTarget.dataset.id
    let username = t.data.username
    let params = JSON.stringify(t.data.params)
    wx.navigateTo({
      url: '../transaction_02/transaction_02?storeUserId=' + storeUserId + '&username=' + username + '&params=' + params,
    })

  },

  // -----弹框层----------
  clickme() {
    this.showModal();
  },
  //显示对话框
  showModal() {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    //在 Y 轴平移的距离
    animation.translateY(300).step()
    this.setData({
      //动画实例的export方法导出动画数据传递给组件的animation属性
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },

  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  //退出登录
  exitLogin() {
    wx.showModal({
      title: '提示',
      content: '是否切换账号？',
      success(res) {
        if (res.confirm) {
          wx.redirectTo({
            url: '../index/index'
          })
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var windowHeight = wx.getSystemInfoSync().windowHeight
    let storeId =''
    let t = this
    let params=''
    if (options.params){
      params  = JSON.parse(options.params)
    }
    if (options.storeId) {
      storeId = options.storeId
      params.storeId = storeId
    }
    if (wx.getStorageSync('token')) { //已登录
      let {
        page,
        size
      } = t.data

      wx.setNavigationBarTitle({
        title: app.globalData.name
      })
      let myData = new Date();
      let getFullYear = myData.getFullYear();
      let getMonth = myData.getMonth() + 1;
      let getDate = myData.getDate();
      if (getDate < 10) {
        getDate = '0' + getDate
      }
      if (getMonth < 10) {
        getMonth = '0' + getMonth
      }
      let currentDate = getFullYear + '-' + getMonth + '-' + getDate; //年月日
      let currentMonth = getFullYear + '-' + getMonth  //年月
      let currentYear = getFullYear //年
      if (!params){
        params={
          orderType:0,
          quertType:0,
          queryTime: currentDate
        }
      }

      t.transaction(page, size, params)
      t.setData({
        date: currentDate,
        endDate: currentDate,
        username: wx.getStorageSync("username"),
        type: options.type,
        itemDate: params.queryTime,
        currentMonth,
        currentYear,
        currentDate,
        storeId,
        params,
        scrollHight: windowHeight - 219
      })
    } else { //未登录
      wx.showToast({
        title: '您还未登录，请先登录！',
        icon: 'none',
        duration: 500
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '../index/index',
        })
      }, 700)

    }


  },



  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    let t = this
    let page = 1
    let {
      // page,
      size,
      params
    } = t.data
    if (this.data.params) {
      t.setData({
        newList: []
      })
    }

    t.transaction(page, size, params)
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let t = this
    var page = t.data.page + 1;
    t.setData({
      page,
    })

    if (page > Math.ceil(this.data.total / this.data.size)) {
      wx.showToast({
        title: '到底了！！！',
        icon: 'none'
      })
      return;
    };
    var page = t.data.page;
    let params = t.data.params
    this.transaction(page, this.data.size, params)

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})